﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace HY.Web.Areas.System.Controllers
{
    public class SysDeptController : BaseController
    {
        SysDeptService service = new SysDeptService();
        // GET: System/SysDept
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult List(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var deptName = Request.Form["deptName"];
                Expression<Func<SysDept, bool>> where = (m => m.IsDeleted == false);
                if (!string.IsNullOrWhiteSpace(deptName))
                    where = where.And(m => m.DeptName.Contains(deptName));
                List<OrderByExpression> order = new List<OrderByExpression>();
                order.Add(new OrderByExpression() { PropertyName = PropertySupport.GetPropertyName<SysDept>(a => a.DeptSort) });
                order.Add(new OrderByExpression() { PropertyName = PropertySupport.GetPropertyName<SysDept>(a => a.CreateDate), IsDesc = true });
                var tuple = service.EFGetPageList(where, page, limit, order.ToArray());
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error(new LogInfo() { UserID = UserInfo.UserID, LogType = LogType.Select, LogMessage = "查询单位列表" }, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Add(SysDept model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Insert };
            try
            {
                model.CreateUserID = base.UserInfo.UserID;
                model.CreateDate = DateTime.Now;
                returnModel.count = service.Add(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"部门{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"部门{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Edit(int? id)
        {
            var model = service.GetById<SysDept>(id);
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(SysDept model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Update };
            try
            {
                var oldModel = service.GetById<SysDept>(model.ID);
                model.IsDeleted = oldModel.IsDeleted;
                model.CreateDate = oldModel.CreateDate;
                model.CreateUserID = oldModel.CreateUserID;
                model.LastModifyUserID = base.UserInfo.UserID;
                model.LastModifyDate = DateTime.Now;
                returnModel.count = service.Update(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"部门{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"部门{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }
    }
}